package org.yylnb.openplatform.webmaster.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.yylnb.openplatform.commons.bean.PayMQBean;
import org.yylnb.openplatform.commons.utlis.CommonsResult;
import org.yylnb.openplatform.webmaster.entity.Recharge;
import org.yylnb.openplatform.webmaster.service.CustomerService;
import org.yylnb.openplatform.webmaster.service.RechargeService;
import org.yylnb.openplatform.webmaster.service.api.CacheService;

import java.util.Date;
import java.util.Map;

/**
 * @author RainLin
 * @date 2020/7/28 - 21:20
 */
@Component
public class SubPaymentListener {
    private static final String APICUSTOMER_REDIS_PREKEY = "APICUSTOMER:";
    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private CacheService cacheService;

    @Autowired
    private CustomerService customerService;
    @StreamListener("paymentresult")
    public void onMessage(PayMQBean payMQBean) {
        System.err.println("收到支付的结果了");
        String oid = payMQBean.getOid();
        Recharge recharge = rechargeService.getRechargeByOid(oid);
        if (recharge != null) {
            recharge.setUpdatetime(new Date());
            recharge.setState(1);
            CommonsResult<Map<String, String>> beanWithObject = cacheService.hGetAll(APICUSTOMER_REDIS_PREKEY + recharge.getCusId());
            Map<String, String> data = beanWithObject.getData();
            if (data != null&&data.size()!=0) {
                //代表数据在缓存中存在
                cacheService.hIncr(APICUSTOMER_REDIS_PREKEY + recharge.getCusId(), "money", payMQBean.getCount() * 10000);
            }else{
                customerService.addMoney(payMQBean.getCount() * 10000,recharge.getCusId());
            }
            rechargeService.updateRecharge(recharge);
        }


    }
}

